package com.sxxm.qxgl.service.impl;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxxm.qxgl.dto.OperateBo;
import com.sxxm.qxgl.entity.SysDept;
import com.sxxm.qxgl.entity.SysRole;
import com.sxxm.qxgl.mapper.SysDeptMapper;
import com.sxxm.qxgl.service.SysDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxxm.qxgl.utils.TreeUtils;
import com.sxxm.qxgl.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author qjw
 * @since 2021-09-14
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
    SysDeptMapper sysDeptMapper;
    public SysDeptServiceImpl(SysDeptMapper sysDeptMapper){
        this.sysDeptMapper=sysDeptMapper;
    }
    @Override
    public Result cx(SysDept operateBo) {

        // 从数据库获取全部数据
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("name", operateBo.getName());
        if (operateBo.getId() != null) {
            queryWrapper.eq("id", operateBo.getId());
            List<SysDept> menus = sysDeptMapper.selectList(queryWrapper);
            return Result.ok().message("成功").data(menus);
        }
        if (operateBo.getStatus().equals("0")) {
            queryWrapper.eq("status", operateBo.getStatus());
        }
        if (operateBo.getStatus().equals("1")) {
            queryWrapper.eq("status", operateBo.getStatus());
        }
        List<SysDept> menus = sysDeptMapper.selectList(queryWrapper);

//        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
//        // 自定义属性名 都要默认值的
//        treeNodeConfig.setWeightKey("order");
//        treeNodeConfig.setIdKey("rid");
//        // 最大递归深度
//        treeNodeConfig.setDeep(3);
        // 调用转换方法
        List<Tree<String>> treeNodes = TreeUtil.build(menus, "0",
                (treeNode, tree) -> {
                    tree.setId(String.valueOf(treeNode.getId()));
                    tree.setParentId(String.valueOf(treeNode.getParentId()));
                    tree.setWeight(treeNode.getSortCode());
                    tree.setName(treeNode.getName());
                    tree.putExtra("status",treeNode.getStatus());
                    // 扩展属性 ...
//                    tree.putExtra("extraField", 666);
//                    tree.putExtra("other", new Object());
                });
//        List<SysDept> ros = TreeUtil.toTree(rootMemuVOS);
        return Result.ok().message("成功").data(treeNodes);
    }

    @Override
    public Result ModifyThe(SysDept sysUser) {
        QueryWrapper<SysDept> Wrapper = new QueryWrapper<>();
        Wrapper.lambda().eq(SysDept::getId, sysUser.getId());
        //修改条件
        SysDept user = sysDeptMapper.selectOne(Wrapper);
        sysUser.setId(user.getId());
        int a = sysDeptMapper.updateById(sysUser);
        if (a <= 0) {
            return Result.error().message("修改失败");
        }
        return Result.ok().message("修改成功");
    }

    @Override
    public Result Addition(SysDept sysUser) {
        if (sysDeptMapper.insert(sysUser) <= 0) {
            return Result.error().message("添加失败");
        }
        return Result.ok().message("添加成功");
    }

    @Override
    public Result Delete(long id) {
        if (sysDeptMapper.deleteById(id) <= 0) {
            return Result.error().message("删除失败");
        }
        return Result.ok().message("删除成功");
    }

    @Override
    public Result zt(long id) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDept::getId, id);
        //修改条件
        SysDept user = sysDeptMapper.selectOne(queryWrapper);
        SysDept user1 = new SysDept();
        user1.setId(user.getId());
        if (sysDeptMapper.selectById(id).getStatus().equals("0")) {
            user1.setStatus("1");
        }else {
            user1.setStatus("0");
        }
        if (sysDeptMapper.updateById(user1) <= 0) {
            return Result.error().message("状态修改失败");
        }
        return Result.ok().message(blzt(user.getId(),user1));
    }

    public String blzt(Long id,SysDept user1){
        QueryWrapper<SysDept> query = new QueryWrapper<>();
        query.eq("parentId",id);
        List<SysDept> a = sysDeptMapper.selectList(query);
        for (int i =0;  i<a.size();i++){
            System.out.println(a.get(i).getId());
            user1.setId(a.get(i).getId());
            if (sysDeptMapper.updateById(user1) <= 0) {
                return "状态修改失败";
            }
            blzt(a.get(i).getId(),user1);
        }
        return "状态修改成功";
    }
}
